package com.everflourish.yeah100.utils;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;

/**
 * SharedPreferences工具内
 * 
 * @author pf
 * @date 2015-3-23
 */
public class SPUtil {

	/** 第一次使用指南 */
	public static final String IS_FIRST_GUIDE = "isFirstGuide";
	/** 第一次使用 */
	public static final String IS_FIRST_USE = "isFirstUse";
	/** 是否展开 */
	public static final String IS_SHRINK = "isShrink";
	/** 第一次登陆 */
	public static final String FIRST_LOGIN = "firstLogin";
	/** 用户名 */
	public static final String USER_NAME = "j_username";
	/** 密码 */
	public static final String PASSWORD = "j_password";
	/** 是否有新的班级 */
	public static final String HAS_NEW_CLASS = "hasNewClass";
	/** 静音 */
	public static final String MUTE = "mute";
	/** 震动 */
	public static final String VIBRATION = "vibration";
	/** 是否是老师 */
	public static final String IS_CHECKED_TEACHER = "isCheckedTheacher";

	public static final String FILE_NAME = "yeah100.cfg";
	private static SharedPreferences mSharedPreferences;

	/**
	 * 获取实例 默认获取的是当前用户的配置的文件
	 * 
	 * @param context
	 *            上下文
	 * @return
	 */
	private SPUtil(Context context) {
		if (Yeah100.loginInfo == null
				|| Yeah100.loginInfo.userInfo == null) {
			return;
		}
		String fileName = Yeah100.loginInfo.userInfo.name + ".cfg";
		mSharedPreferences = context.getSharedPreferences(fileName,
				Context.MODE_PRIVATE);
	}

	/**
	 * 返回SharedPreferencesUtil
	 * @param context
	 * @return
	 */
	public static SPUtil getSharedPreferences(Context context){
		return new SPUtil(context);
	}
	/**
	 * 返回SharedPreferencesUtil
	 * @param context
	 * @param fileName
	 * @return
	 */
	public static SPUtil getSharedPreferences(Context context, String fileName){
		return new SPUtil(context, fileName);
	}
	
	
	/**
	 * 
	 * 一般存放公共的参数，如需保存最后一次的帐号跟密码
	 * 
	 * @param context
	 *            上下文
	 * @param fileName
	 *            配置文件的名字，这里一般指 public static final String FILE_NAME =
	 *            "yeah100.cfg"; 共用参数
	 * @return
	 */
	private SPUtil(Context context, String fileName) {
		mSharedPreferences = context.getSharedPreferences(fileName,
				Context.MODE_PRIVATE);
	}

	/**
	 * 写入键值对 key=value
	 * 
	 * @param key
	 * @param value
	 * @return
	 */
	public boolean putString(String key, String value) {
		Editor editor = mSharedPreferences.edit();
		editor.putString(key, value);
		return editor.commit();
	}
	/**
	 * 写入键值对 key=value
	 * 
	 * @param key
	 * @param value
	 * @return
	 */
	public boolean putLong(String key, long value) {
		Editor editor = mSharedPreferences.edit();
		editor.putLong(key, value);
		return editor.commit();
	}

	/**
	 * 写入键值对 key=value
	 * 
	 * @param key
	 * @param value
	 * @return
	 */
	public boolean putBoolean(String key, boolean value) {
		Editor editor = mSharedPreferences.edit();
		editor.putBoolean(key, value);
		return editor.commit();
	}

	/**
	 * 获取key的值
	 * 
	 * @param key
	 * @param defValue
	 *            默认值
	 * @return
	 */
	public String getString(String key, String defValue) {
		return mSharedPreferences.getString(key, defValue);
	}
	/**
	 * 获取key的值
	 * 
	 * @param key
	 * @param defValue
	 *            默认值
	 * @return
	 */
	public long getLong(String key, long defValue) {
		return mSharedPreferences.getLong(key, defValue);
	}

	/**
	 * 获取key的值
	 * 
	 * @param key
	 * @param defValue
	 *            默认值
	 * @return
	 */
	public boolean getBoolean(String key, boolean defValue) {
		return mSharedPreferences.getBoolean(key, defValue);
	}

	public boolean remove(String key) {
		Editor edit = mSharedPreferences.edit();
		edit.remove(key);
		return edit.commit();
	}
}
